package org.logsin37.tool.eurekamanager.app.service.impl;

import lombok.AllArgsConstructor;
import org.logsin37.tool.eurekamanager.app.service.EurekaService;
import org.logsin37.tool.eurekamanager.domain.entity.Eureka;
import org.logsin37.tool.eurekamanager.domain.service.EurekaRemoteDomainService;
import org.logsin37.tool.eurekamanager.infra.repository.EurekaRepository;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;

/**
 * Eureka Service Impl
 *
 * @author logsin37 2020/01/20 17:46
 */
@Service
@AllArgsConstructor
public class EurekaServiceImpl implements EurekaService {

    private final EurekaRemoteDomainService eurekaRemoteDomainService;
    private final EurekaRepository eurekaRepository;

    /**
     * 保存Eureka
     *
     * @param eureka eureka信息
     * @return 处理结果
     */
    @Override
    public Mono<Void> saveEureka(Eureka eureka) {
        return this.eurekaRepository.saveEureka(eureka)
                .subscribeOn(Schedulers.elastic())
                .publishOn(Schedulers.elastic())
                .doOnNext(e -> this.eurekaRemoteDomainService.register(eureka))
                .then();
    }

    /**
     * 根据Code删除Eureka信息
     *
     * @param code code
     * @return 删除结果
     */
    @Override
    public Mono<Void> removeEurekaByCode(String code) {
        return this.eurekaRepository.removeEurekaByCode(code)
                .subscribeOn(Schedulers.elastic())
                .publishOn(Schedulers.elastic())
                .doOnNext(e -> this.eurekaRemoteDomainService.unregister(code));
    }
}
